package com.dq.service.activity.impl;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.ActivityQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.activity.Activity;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.repository.BaseRepositories;
import com.dq.service.activity.ActivityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2020/3/13 13:09
 */
@Service
public class ActivityServiceImpl extends BaseRepositories implements ActivityService{
    @Override
    public Vo list(ActivityQuery activityQuery) throws AdminException {
        activityQuery.format();
        Map<String,Object> query = activityQuery.parseMap();
        if(activityQuery.getOrgId() != null){
            query.put("org_id",activityQuery.getOrgId());
            query.remove("orgId");
        }
        if(activityQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr, activityQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(activityQuery.getEndTime()!=null){
            query.put("createTime"+ BaseDaoImpl.lteSqlStr, activityQuery.getEndTime());
            query.remove("endTime");
        }
        if(StringUtils.isNotEmpty(activityQuery.getTitle())){
            query.put("title"+BaseDaoImpl.likeSqlStr,"%"+activityQuery.getTitle()+"%");
            query.remove("title");
        }
        List<Activity> activityList = dao.find(dao.query2Hql(Activity.class,query),query);
        Long count = dao.count(dao.count2Hql(Activity.class,query),query);
        PageHelper.startPage(activityQuery.getPage(),activityQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<Activity.Vo>) VO.toVOCollection(activityList));
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }
}
